module Fastlane
  module Actions
    module SharedValues
      DING_TALK_CUSTOM_VALUE = :DING_TALK_CUSTOM_VALUE
    end

    class DingTalkAction < Action
      def self.run(params)
        dingTalkUrl = params[:ding_talk_url]
        useSSL = params[:use_ssl]
        contentTitle = params[:content_title]
        contentMessage = params[:content_message]
        appIconUrl = params[:app_icon_url]
        messageUrl = params[:message_url]
        uri1 = URI.parse(dingTalkUrl)
        https1 = Net::HTTP.new(uri1.host, uri1.port)
        https1.use_ssl = useSSL
        request1 = Net::HTTP::Post.new(uri1.request_uri)
        request1.add_field('Content-Type', 'application/json')
        markdown =
        {
          msgtype: "link",
          link: {
            title: "#{contentTitle}",
            text: "#{contentMessage}",
            picUrl: "#{appIconUrl}",
            messageUrl: "#{messageUrl}"
          }
        }
        request1.body = markdown.to_json
        response1 = https1.request(request1)
        puts "Response1 #{response1.code} #{response1.message}: #{response1.body}"
      end

      #####################################################
      # @!group Documentation
      #####################################################

      def self.description
        "A short description with <= 80 characters of what this action does"
      end

      def self.details
        # Optional:
        # this is your chance to provide a more detailed description of this action
        "You can use this action to do cool things..."
      end

      def self.available_options
        # Define all options your action supports.

        # Below a few examples
        [
          FastlaneCore::ConfigItem.new(key: :ding_talk_url,
                                       description: "dingtalk url", # a short description of this parameter
                                       is_string: false,
                                       default_value: true),
          FastlaneCore::ConfigItem.new(key: :use_ssl,
                                       description: "是否https",
                                       is_string: false, # true: verifies the input is a string, false: every kind of value
                                       default_value: false), # the default value if the user didn't provide one
          FastlaneCore::ConfigItem.new(key: :content_title,
                                     description: "内容title", # a short description of this parameter
                                     is_string: false,
                                     default_value: true),
         FastlaneCore::ConfigItem.new(key: :content_message,
                                     description: "内容消息",
                                     is_string: false, # true: verifies the input is a string, false: every kind of value
                                     default_value: false), # the default value if the user didn't provide one
          FastlaneCore::ConfigItem.new(key: :app_icon_url,
                                       description: "app图标的url", # a short description of this parameter
                                       is_string: false,
                                       default_value: true),
          FastlaneCore::ConfigItem.new(key: :message_url,
                                       description: "连接地址",
                                       is_string: false, # true: verifies the input is a string, false: every kind of value
                                       default_value: false) # the default value if the user didn't provide one

        ]
      end

      def self.output
        # Define the shared values you are going to provide
        # Example
        [
          ['DING_TALK_CUSTOM_VALUE', 'A description of what this value contains']
        ]
      end

      def self.return_value
        # If your method provides a return value, you can describe here what it does
      end

      def self.authors
        # So no one will ever forget your contribution to fastlane :) You are awesome btw!
        ["芝加哥兔兔养殖场"]
      end

      def self.is_supported?(platform)
        # you can do things like
        #
        #  true
        #
        #  platform == :ios
        #
        #  [:ios, :mac].include?(platform)
        #

        platform == :ios
      end
    end
  end
end
